<html>
  <head>
     <link rel="stylesheet" type="text/css" href="styles.css"> 
    <script src="util.js" type="text/javascript"></script> 
    <script>
function addServer() {
  try {
    var servers = JSON.parse(localStorage.servers || "[]");
    var serverName = $F('serverText');
  
    servers.forEach(function(server) {
      if (server.name == serverName) {
        throw "Connection to " + serverName + " already established";
      }
    });

    var portValue = parseInt($F('serverPort'));
    if (isNaN(portValue)) {
      throw $F('serverPort') + " is not a valid port value";
    }

    var nickValue = $F('nick');
    var newServer = {
      name: serverName,
      port: portValue,
      nick: nickValue,
      channels: []
    };
    
    servers.push(newServer);
    
    localStorage.servers = JSON.stringify(servers);
    window.opener.addServerConnection(newServer);
    close();
  } catch (ex) {
    alert(ex);
  }
}
    </script>
  </head>
  <body>
    <div>
      <input id="serverText" type="text" value="irc.freenode.net">
      <input id="serverPort" type="text" value="6667">
    </div>
    <div>
      <input id="nick" type="text" value="nick">
    </div>
    <div>
      <input type="button" value="Add New Server"
             onclick="addServer();">
    </div>
  </body>
</html>
